package router

import (
	"GOPAAS/internal/utils"
	"github.com/gin-contrib/multitemplate"
	"html/template"
	"io/ioutil"
)

const (
	TEMP_DIR = "./web/template"
)

func createMyRender(r multitemplate.Renderer) multitemplate.Renderer {
	base_templates := []string{
		TEMP_DIR + "/public/layout.html",
		TEMP_DIR + "/public/header.html",
		TEMP_DIR + "/public/footer.html",
		TEMP_DIR + "/public/menu.html",
		TEMP_DIR + "/public/script.html",
		TEMP_DIR + "/public/nav.html",
	}
	base_dialog_templates := []string{
		TEMP_DIR + "/public/layout_dialog.html",
		TEMP_DIR + "/public/header.html",
		TEMP_DIR + "/public/script.html",
	}
	dirList, err := ioutil.ReadDir(TEMP_DIR)
	if err != nil {
		panic(err)
	}
	for _, fi := range dirList {
		dir_name := fi.Name()
		if fi.IsDir() {
			if dir_name == "public" {
				continue
			}
			fileList, _ := ioutil.ReadDir(TEMP_DIR + "/" + dir_name)
			for _, f := range fileList {
				if !f.IsDir() {
					var temp []string
					if dir_name == "dialog" {
						temp = base_dialog_templates
					} else {
						temp = base_templates
					}
					page := append(temp, TEMP_DIR+"/"+dir_name+"/"+f.Name())
					r.AddFromFilesFuncs(dir_name+"."+f.Name(), template.FuncMap{
						"Int64InArr": utils.Int64InArray,
					}, page...)
				}
			}
		} else {
			r.AddFromFiles(dir_name, TEMP_DIR+"/"+dir_name)
		}
	}
	return r
}
